package com.tobby.camel.camelinaction.chapter5.errorhandler;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.camel.Exchange;
import org.w3c.dom.Document;

public class OrderService {

	public String validate(String body) throws OrderValidationException {
		// an order must contain an amount
		if (!body.contains("amount")) {
			throw new OrderValidationException("Invalid Order");
		}
		// attach the order id
		return body + ",id=123";
	}

	public String enrich(String body) throws OrderException {
		if (body.contains("ActiveMQ in Action")) {
			throw new OrderException("ActiveMQ in Action is out of stock");
		}
		return body + ",status=OK";
	}

	public String toCsv(String body) throws OrderException {
		if (body.contains("xml")) {
			throw new OrderException("xml files not allowed");
		}
		return body.replaceAll("#", ",");
	}

	public void toSoap(Exchange exchange) throws IOException {
		String body = exchange.getIn().getBody(String.class);
		if (body.contains("ActiveMQ in Action")) {
			// load the soapFault.xml into a DOM
			InputStream is = exchange.getContext().getClassResolver()
					.loadResourceAsStream(
							"com/tobby/camel/camelinaction/chapter5/errorhandler/soapFault.xml");
			Document dom = exchange.getContext().getTypeConverter()
					.convertTo(Document.class, is);
			BufferedReader reader = new BufferedReader(new InputStreamReader(exchange.getContext().getClassResolver()
					.loadResourceAsStream(
							"com/tobby/camel/camelinaction/chapter5/errorhandler/soapFault.xml")));
			String output = "";
			while (true) {
				String inline = reader.readLine();
				if (inline == null) break;
				output += inline;
			}
			System.out.println(output);
			// set a fault to indicate a failure
			exchange.getOut().setFault(true);
			exchange.getOut().setBody(dom);
		}
		else {
			// load the soapOk.xml into a DOM
			InputStream is = exchange.getContext().getClassResolver()
					.loadResourceAsStream(
							"com/tobby/camel/camelinaction/chapter5/errorhandler/soapOk.xml");
			Document dom = exchange.getContext().getTypeConverter()
					.convertTo(Document.class, is);

			// set a xml reply
			exchange.getOut().setBody(dom);
		}
	}

}
